package com.xgxx.chendawei.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgxx.chendawei.system.entity.Teacher;
import com.xgxx.chendawei.system.service.TeacherService;
import com.xgxx.chendawei.system.utils.api.ApiResponse;
import com.xgxx.chendawei.system.utils.api.ApiResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;


/**
 * Teacher Controller 实现类
 */
@RestController
@RequestMapping("/teacher")
@Api(tags = "教师信息")
@Slf4j
public class TeacherController extends AbstractBaseController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "教师信息-demo")
    @GetMapping(value = "/get")
    @Transactional
    public ApiResponse get() {
        for (int i = 0; i < 100; i++) {
            Teacher t = new Teacher();
            t.setTeacherName("张三" + i);
            t.setTeacherCode("0000" + i);
            teacherService.save(t);
        }
        Page<Teacher> page = new Page<>(2, 2);
        Page<Teacher> TeacherPage = teacherService.page(page, null);
        System.out.println(TeacherPage);
        List<Teacher> Teachers = page.getRecords();//分页后的数据
        Teachers.forEach(System.out::println);
        System.out.println("总页数：" + TeacherPage.getPages());
        System.out.println("总条数：" + TeacherPage.getTotal());
        System.out.println("当前页：" + TeacherPage.getCurrent());
        System.out.println("当前页显示条数：" + TeacherPage.getSize());
        System.out.println("是否有下一页：" + TeacherPage.hasNext());
        System.out.println("是否有上一页：" + TeacherPage.hasPrevious());

        System.out.println("================");

        Teacher m = new Teacher();
        m.setTeacherName("张");
        Page<Teacher> p = teacherService.selectPage(page, m);
        System.out.println(p);
        List<Teacher> pps = page.getRecords();//分页后的数据
        pps.forEach(System.out::println);
        System.out.println("总页数：" + p.getPages());
        System.out.println("总条数：" + p.getTotal());
        System.out.println("当前页：" + p.getCurrent());
        System.out.println("当前页显示条数：" + p.getSize());
        System.out.println("是否有下一页：" + p.hasNext());
        System.out.println("是否有上一页：" + p.hasPrevious());
        return ApiResponseUtil.getCommErrorApiResponse("demo");
    }


    @PutMapping(value = "/add")
    @ApiOperation(value = "教师信息-新增")
    public ApiResponse test(@RequestBody @NotNull Map<String, Object> model) {

        return ApiResponseUtil.getCommErrorApiResponse("新增失败");
    }
}
